/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-11-25，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.ui.adapter;

import java.util.List;
import com.chinamobile.ln.ictenterprises.R;
import com.chinamobile.ln.ictenterprises.model.response.ResponseHistory.Records;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OverSendAdapter extends BaseAdapter
{
    private Context context;
    private LayoutInflater mInflater;
    public List<Records> list;
    private Handler handler;
    public String currPage;
    public String allPage;

    public OverSendAdapter(Context context, List<Records> list, Handler handler,
        String currPage, String allPage)
    {
        this.context = context;
        this.list = list;
        this.mInflater = LayoutInflater.from(context);
        this.allPage = allPage;
        this.currPage = currPage;
        this.handler = handler;
    }

    @Override
    public int getCount()
    {
        int size = list.size();
        if (Integer.parseInt(allPage) > Integer.parseInt(currPage))
        {
            return size + 1;
        }
        else
        {
            return size;
        }
    }

    @Override
    public boolean isEnabled(int position)
    {
        if (position >= list.size())
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (position >= list.size())
        {
            handler.sendMessage(handler.obtainMessage(-1, position));
            convertView = LayoutInflater.from(context).inflate(
                R.layout.list_wait, null);
            convertView.setTag(null);
            convertView.setEnabled(false);
            return convertView;
        }
        else
        {
            Holder holder;
            if (convertView != null && convertView.getTag() != null)
            {
                holder = (Holder) convertView.getTag();
            }
            else
            {
                holder = new Holder();
                convertView = mInflater.inflate(R.layout.history_item, null);
                holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
                holder.tv_name = (TextView) convertView
                    .findViewById(R.id.tv_name);
                holder.tv_content = (TextView) convertView
                    .findViewById(R.id.tv_content);
                holder.tv_date = (TextView) convertView
                    .findViewById(R.id.tv_date);
                convertView.setTag(holder);
            }

            Records records = list.get(position);
            holder.tv_id.setText(records.id);
            holder.tv_name.setText(records.addressList.replaceAll(",", "、"));
            holder.tv_content.setText(records.msg);
            holder.tv_date.setText(records.time);
            return convertView;
        }
    }

    private final class Holder
    {
        TextView tv_id, tv_name, tv_content, tv_date;
    }

}
